package org.opens.converter.mybatisJdbcType.handle;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.opens.converter.mybatisJdbcType.I_MybatisJdbcTypeConverter;
import org.opens.converter.mybatisJdbcType.MybatisJdbcTypeConverterEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述
 *
 * @author by zhangyuming
 * @date 2023/3/7
 */
public class MysqlMybatisJdbcTypeConverterHandle implements I_MybatisJdbcTypeConverter {

    private static Log log = LogFactory.get(MysqlMybatisJdbcTypeConverterHandle.class);

    private static Map<String, MybatisJdbcTypeConverterEntity> converterMap = new HashMap<>();

    static {
        converterMap.put("bit", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("BOOLEAN").build());
        converterMap.put("boolean", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("BOOLEAN").build());
        converterMap.put("tinyint", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("TINYINT").build());
        converterMap.put("smallint", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("INTEGER").build());
        converterMap.put("int", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("INTEGER").build());
        converterMap.put("bigint", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("BIGINT").build());
        converterMap.put("float", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("FLOAT").build());
        converterMap.put("double", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("DOUBLE").build());
        converterMap.put("decimal", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("DECIMAL").build());
        converterMap.put("varchar", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("VARCHAR").build());
        converterMap.put("char", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("CHAR").build());
        converterMap.put("datetime", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("TIMESTAMP").build());
        converterMap.put("blob", MybatisJdbcTypeConverterEntity.builder().jdbcTypeName("BLOB").build());
    }

    @Override
    public MybatisJdbcTypeConverterEntity dbType2JdbcType(String dbSqlType) {
        MybatisJdbcTypeConverterEntity converterEntity = converterMap.get(dbSqlType);
        if (converterEntity == null) {
            log.warn("未配置的sqlType类型对应的jdbcType类型: {}, 使用默认值: VARCHAR!", dbSqlType);
            return MybatisJdbcTypeConverterEntity.getDefault();
        }
        return converterMap.get(dbSqlType);
    }

}
